#include "CircuitGraph.h"

CircuitGraph::CircuitGraph()
{
    for(auto x:inDegree)
    {
        x=0;
    }
}
void CircuitGraph::setEdge(int from,int to)
{

    EdgeNode *p=this->gates[from].first;//初始化该边指向VNode节点的第一条边
    EdgeNode *q=NULL;//指向p节点前面的那个VNode节点
    while(p!=NULL)
    {
        if(p->adjVex==to)//重复边检验
            return;
        q=p;
        p=p->next;
    }
    this->gates[to].gate.curlen++;//被指向的门的输入个数加1
    this->inDegree[to]+=1;//被指向的节点入度加1
     EdgeNode *newEdge=new EdgeNode;
    if(q==NULL)//如果VNode节点没有第一条边
    {
        this->gates[from].first=newEdge;
        newEdge->adjVex=to;
        return;
    }
    newEdge->adjVex=to;
    q->next=newEdge;
}

void CircuitGraph::topology()//拓扑赋值
{
    QStack<int> stk;
    EdgeNode *p;
    //将入度为零的节点序号压入栈中
    for(auto x:inDegree)
    {
        if(x==0)
            stk.push(x);
    }
    //当栈不空时
    while(!stk.isEmpty())
    {
        //栈顶元素出栈
        int temp=stk.top();
        stk.pop();
        inDegree[temp]=-1;
        //遍历所以有邻接点，所有点入度减一
        p=gates[temp].first;
        while(p!=NULL)
        {
            inDegree[p->adjVex]--;
            //把父节点的输出赋给每个子节点作为输入
            gates[p->adjVex].gate.Inputs[gates[p->adjVex].gate.curlen++]=gates[p->adjVex].gate.Output;
            //如果节点的输入个数到了最高个数，计算输出结果
            if(gates[p->adjVex].gate.curlen==gates[p->adjVex].gate.maxlen)
                gates[p->adjVex].gate.DoSimulate();
            //如果入读减一后为0，入栈
            if(inDegree[p->adjVex]==0)
                stk.push(p->adjVex);
            //
            p=p->next;
        }
    }
}
//遍历，将各个门的坐标与鼠标左键点击信息进行匹配，若匹配则状态置一
void CircuitGraph::selectGate_1(int mouse_x,int mouse_y)
{
   int i;
    for(i=0;i<this->gates.length();i++)
    {
        if(mouse_x > this->gates[i].gate.gate_x && mouse_x <this->gates[i].gate.gate_x+50&&mouse_y > this->gates[i].gate.gate_y && mouse_y <this->gates[i].gate.gate_y+50)
        {
           // this->gates[i].gate.isSelected=true;
            if(this->gates[i].gate.clickState==0 )
                this->gates[i].gate.clickState=1;
            qDebug()<<"鼠标按下了！！";
            qDebug()<<this->gates[i].gate.clickState;

            break;
        }
    }
}
void CircuitGraph::selectGate_2(int mouse_x,int mouse_y)//初步测试无误
{
    int i;
    bool clearFlag=true;
    for(i=0;i<this->gates.length();i++)
   // for(i=this->gates.length()-1;i>=0;i--)
    {
        if(mouse_x > this->gates[i].gate.gate_x && mouse_x <this->gates[i].gate.gate_x+50&&mouse_y > this->gates[i].gate.gate_y && mouse_y <this->gates[i].gate.gate_y+50)
        {
            clearFlag=false;//
            if(this->gates[i].gate.clickState==1)//如果之前press了这个按钮
                this->gates[i].gate.clickState=2;
            else//如果之前没press这个按钮，考虑连接两个门
                this->gates[i].gate.clickState=3;
            qDebug()<<"鼠标松开了！！";
            qDebug()<<this->gates[i].gate.clickState;
            break;
        }
       // else if(this->gates[i].gate.clickState!=2)
    }

    if(clearFlag)
    {
         for(int j=0;j<this->gates.length();j++)
             this->gates[j].gate.clickState=0;
    }
}

//void CircuitGraph::deselectGate()
//{
//     for(int i=0;i<this->gates.length();i++)
//     {
//         gates[i].gate.isSelected=false;
//     }
//}
void CircuitGraph::gateMove(int x,int y,int i)
{
    gates[i].gate.gate_x+=(x-gates[i].gate.gate_x);
    gates[i].gate.gate_y+=(y-gates[i].gate.gate_y);
}
